package com.xja.service.impl;

import com.xja.common.JsonBean;
import com.xja.entity.SysDeptMenu;
import com.xja.entity.SysMenu;
import com.xja.mapper.SysDeptMenuMapper;
import com.xja.mapper.SysMenuMapper;
import com.xja.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author CHENCHEN
 * @company 北京学佳澳教育科技
 * @create 2024-01-11 10:54
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysDeptMenuMapper sysDeptMenuMapper;

    @Override
    public JsonBean selectTableList(Integer pId) {

        return new JsonBean(0,"success", sysMenuMapper.selectTableList(pId));
    }

    @Override
    public JsonBean selectTreeList() {
        return new JsonBean(0,"success", sysMenuMapper.selectTreeList());
    }

    @Override
    public JsonBean selectMenuById(Integer id) {
        return new JsonBean(0,"success",sysMenuMapper.selectByPrimaryKey(id));
    }

    @Override
    public JsonBean insertMenu(SysMenu sysMenu) {
        JsonBean jsonBean = new JsonBean(-1, "fail", null);
        int i = sysMenuMapper.insert(sysMenu);
        if(i>0){
            jsonBean.setCode(0);
            jsonBean.setMsg("success");
        }
        return jsonBean;
    }

    @Override
    public JsonBean updateMenu(SysMenu sysMenu) {
        JsonBean jsonBean = new JsonBean(-1, "fail", null);
        int i = sysMenuMapper.updateByPrimaryKeySelective(sysMenu);
        if(i>0){
            jsonBean.setCode(0);
            jsonBean.setMsg("success");
        }
        return jsonBean;
    }

    @Override
    public JsonBean deleteById(Integer id) {
        JsonBean jsonBean = new JsonBean(-1, "fail", null);
        int i = sysMenuMapper.deleteByPrimaryKey(id);
        if(i>0){
            jsonBean.setCode(0);
            jsonBean.setMsg("success");
        }
        return jsonBean;
    }

    @Override
    public JsonBean insertDeptMenu(String ids, Integer deptId) {

        sysDeptMenuMapper.deleteByDeptId(deptId);
        String[] idArr = ids.split(",");
        for (int i=0;i<idArr.length;i++){
            SysDeptMenu sysDeptMenu = new SysDeptMenu();
            sysDeptMenu.setDeptId(deptId);
            sysDeptMenu.setMenuId(Integer.valueOf(idArr[i]));
            sysDeptMenu.setStatus("1");
            sysDeptMenuMapper.insert(sysDeptMenu);
        }

        return new JsonBean(0,"success", null);
    }

    @Override
    public JsonBean getMenuByDeptId(Integer deptId) {
        return new JsonBean(0,"success", sysMenuMapper.selectMenuByDeptId(deptId));
    }
}
